﻿using APJ.ComLib.Model.Context;
using APJ.ComLib.Model.System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;

namespace APJ.ComLib.System
{
    public class SystemParameterRepository : ISystemParameterRepository
    {
        private readonly SystemDbContext _dbContext;

        public SystemParameterRepository() { }

        public SystemParameterRepository(SystemDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        public IEnumerable<SystemParameter> ListAll()
        {
            return _dbContext.SystemParameters.ToList();

        }

        public SystemParameter GetByID(int? id)
        {
            SystemParameter systemParameter = _dbContext.SystemParameters.Find(id);

            return systemParameter;
        }

        public void Add(SystemParameter systemParameter)
        {
            _dbContext.SystemParameters.Add(systemParameter);
            _dbContext.SaveChanges();
        }

        public void Edit(SystemParameter systemParameter)
        {
            _dbContext.Entry(systemParameter).State = EntityState.Modified;
            _dbContext.SaveChanges();

        }

        public void Remove(SystemParameter systemParameter)
        {
            _dbContext.SystemParameters.Remove(systemParameter);
            _dbContext.SaveChanges();

        }

        protected bool disposed;

        protected virtual void Dispose(bool disposing)
        {
            lock (this)
            {
                // Do nothing if the object has already been disposed of.
                if (disposed)
                    return;
                if (disposing)
                {
                    // Release disposable objects used by this instance here.
                    if (_dbContext != null)
                        _dbContext.Dispose();
                }

                // Release unmanaged resources here. Don't access reference type fields.

                // Remember that the object has been disposed of.
                disposed = true;
            }
        }

        public virtual void Dispose()
        {
            Dispose(true);
            // Unregister object for finalization.
            GC.SuppressFinalize(this);
        }
    }
}
